<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

/**
 * 广告控制器
 */
class Ad extends AdminBase
{
    
    /**
     * 广告列表
     */
    public function adList()
    {
        $this->adCommon();
        $where = $this->logicAd->getWhere($this->param);
        
        $this->assign('list', $this->logicAd->getAdList($where, 'a.*,m.nickname,c.title as category_name', 'a.create_time desc'));
        
        return $this->fetch('ad_list');
    }
    
    /**
     * 广告添加
     */
    public function adAdd()
    {
        
        $this->adCommon();
        return $this->fetch('ad_edit');
    }
    
    /**
     * 广告编辑
     */
    public function adEdit()
    {
        
        $this->adCommon();
        
        $info = $this->logicAd->getAdInfo(['a.id' => $this->param['id']], 'a.*,m.nickname,c.title as category_name');
        
       // !empty($info) && $info['img_ids_array'] = str2arr($info['img_ids']);
        
        $this->assign('info', $info);
        return $this->fetch('ad_edit');
    }
    
    /**
     * 广告添加与编辑通用方法
     */
    public function adCommon()
    {
        
        IS_POST && $this->jump($this->logicAd->adEdit($this->param));
        
        $this->assign('ad_cat_list', $this->logicAd->getAdCatList([], 'id,title', '', false));
    }
    
    /**
     * 广告分类添加
     */
    public function adCatAdd()
    {
        
        IS_POST && $this->jump($this->logicAd->adCatEdit($this->param));
        $list=db('goods_cat')->where('pid',0)->select();
        $this->assign('cat_list', $list);
        return $this->fetch('ad_cat_edit');
    }
    
    /**
     * 广告分类编辑
     */
    public function adCatEdit()
    {
        
        IS_POST && $this->jump($this->logicAd->adCatEdit($this->param));
        
        $info = $this->logicAd->getAdCatInfo(['id' => $this->param['id']]);
        
        $this->assign('info', $info);
        $list=db('goods_cat')->where('pid',0)->select();
        $this->assign('cat_list', $list);
        return $this->fetch('ad_cat_edit');
    }
    
    /**
     * 广告分类列表
     */
    public function adCatList()
    {
        $list=$this->logicAd->getAdCatList();
        foreach($list as $k=>$v){
            $list[$k]['ctitle']=db('goods_cat')->where('id',$v['catid'])->value('title');
        }
        $this->assign('list',$list );
       
        return $this->fetch('ad_cat_list');
    }
    
    /**
     * 广告分类删除
     */
    public function adCatDel($id = 0)
    {
        
        $this->jump($this->logicAd->adCategoryDel(['id' => $id]));
    }
    
    /**
     * 数据状态设置
     */
    public function setStatus()
    {
        
        $this->jump($this->logicAdminBase->setStatus('Ad', $this->param));
    }
}
